package com.burgess.net.queue;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * 消息接受者
 * 创建人    BurgessLee
 * 创建时间   2020/5/19
 * 描述
 */
public class JmsReceiver {

    public static void main(String[] args) throws JMSException {

        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
        Connection connection = activeMQConnectionFactory.createConnection();
        connection.start();

        final Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
        Destination destination = session.createQueue("hello-world");
        MessageConsumer consumer = session.createConsumer(destination);
        int i = 0;
        while(i < 3){
            i ++;
            TextMessage receive = (TextMessage) consumer.receive();
            session.commit();
            System.out.println("收到消息：" + receive.getText());
        }

        session.close();
        connection.close();
    }
}
